#include "appbutton.h"

#include <QDebug>

AppButton::AppButton(QWidget *parent)
    : QToolButton(parent)
{
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &AppButton::mouseSingleClickEvent);

    menu = new QMenu(this);
    open = new QAction(tr("打开"), this);
    del = new QAction(tr("删除"), this);
    menu->addAction(open);
    menu->addAction(del);

    connect(open, &QAction::triggered, [=]{
        emit clicked();
    });
    connect(del, &QAction::triggered, [=]{
        emit delBtn();
    });

    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, &AppButton::customContextMenuRequested, [=](const QPoint &pos){
//        qDebug()<<pos;
        menu->exec(QCursor::pos());
    });
}

AppButton::~AppButton()
{
}

void AppButton::focusInEvent(QFocusEvent *event)
{
    setStyleSheet("QToolButton{background-color:rgba(240,255,255, 50%);border:flat}");
}

void AppButton::focusOutEvent(QFocusEvent *event)
{
    setStyleSheet("QToolButton{background-color:transparent}");
}

void AppButton::mouseSingleClickEvent()
{
    timer->stop();
}


void AppButton::mouseDoubleClickEvent(QMouseEvent *event)
{
    timer->stop();
    emit clicked();
}

void AppButton::mousePressEvent(QMouseEvent *event)
{
    if (!hasFocus()) setFocus();
    else clearFocus();

    if (event->button() == Qt::LeftButton)
    {
        timer->start(200);
    }
    else if (event->button() == Qt::RightButton)
    {
        timer->stop();
    }
}
